classSolution { public: intget(int x){ int res = 0; while (x) { res += x % 10; x /= 10; } return res; }
intmovingCount(int m, int n, int k){ vector<vector<bool>> st(m, vector<bool>(n, false)); int res = 0;
queue<pair<int, int>> q; q.push({0, 0});
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; while (q.size()) { auto t = q.front(); q.pop(); int x = t.first, y = t.second; if (st[x][y] || get(x) + get(y) > k) continue;
res ++ ; st[x][y] = true;
for (int i = 0; i < 4; i ++ ) { int a = x + dx[i], b = y + dy[i]; if (a < 0 || a >= m || b < 0 || b >= n) continue; q.push({a, b}); } }